package util;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.Vector;

public class INIGroup {
	public Hashtable<String, String> vals = null;
	public String title;
	
	public INIGroup(Scanner s) throws Exception
	{
		//String title = Util.toUTF8(s.nextLine());
		String title = s.nextLine();
		//System.out.println("Next line : " + title);
		
		if(title.startsWith("[") && title.endsWith("]"))
		{
			this.title = title.substring(1, title.length() - 1);
		}
		else
		{
			throw new Exception("No more groups");
		}
		
		vals = new Hashtable<String, String>();
		while(s.hasNext())
		{
			//String tmp = Util.toUTF8(s.nextLine());
			String tmp = s.nextLine();
			if(tmp.length() < 3) break;
			String parts[] = tmp.split("=");
			if(parts.length > 1)
			{
				vals.put(parts[0], parts[1]);
			}
		}
	}
	
	public int getInteger(String key)
	{
		return getInteger(key, 0);
	}
	
	public int getInteger(String key, int def)
	{
		String s = vals.get(key);
		if(s == null) return def;
		try
		{
			int n = Integer.parseInt(s);
			return n;
		}
		catch(Exception e)
		{
			return def;
		}
	}
	
	public String getString(String key)
	{
		return getString(key, "");
	}
	
	public String getString(String key, String def)
	{
		String s = vals.get(key);
		if(s == null)
			return def;
		else
			return s;
	}
	
	public boolean getBoolean(String key, boolean def)
	{
		String s = vals.get(key);
		if(s != null)
			return "True".equals(s);
		else
			return def;
	}
	
	public void mergeTo(HashMap<String, String> map)
	{
		map.putAll(vals);
	}
	
	public Vector<String> getKeyList()
	{
		Vector<String> vs = new Vector<String>();
		vs.addAll(vals.keySet());
		return vs;
	}
	
	public int[] getIntList(String key)
	{
		String v = getString(key, null);
		if(v == null) return null;
		String[] parts = v.split(";");
		int[] iparts = new int[parts.length];
		for(int i = 0; i < parts.length; ++i)
			iparts[i] = Integer.parseInt(parts[i]);
		return iparts;
	}
	
	public String[] getStringList(String key)
	{
		String v = getString(key, null);
		if(v == null) return null;
		String[] parts = v.split(";");
		return parts;
	}
}
